using System;
using Moq;
using NUnit.Framework;

namespace OpenWaves.UnitTests
{
    [TestFixture]
    public class BasicResolverTests
    {
        [Test]
        public void When_service_is_not_registered_TryResolve_returns_false()
        {
            var resolver = new BasicResolver();

            IService service;
            Assert.That(resolver.TryResolve(out service), Is.False);
        }

        [Test]
        public void When_service_is_registered_TryResolve_returns_true_and_instance_of_registered_service()
        {
            var registeredService = new Mock<IService>().Object;

            var resolver = new BasicResolver();
            resolver.Register<IService>(registeredService);

            IService service;
            Assert.That(resolver.TryResolve(out service), Is.True);
            Assert.That(service, Is.SameAs(registeredService));
        }

        [Test]
        [ExpectedException]
        public void Attempt_to_register_service_that_does_not_implement_contract_throws()
        {
            BasicResolver resolver = new BasicResolver();
            try
            {
                resolver.Register<IService>(new SampleServiceImplementation());
                Assert.Fail("Exception expected");
            }
            catch (InvalidOperationException e)
            {
                Assert.IsTrue(e.Message.Contains(typeof(SampleServiceImplementation).ToString()));
                Assert.IsTrue(e.Message.Contains(typeof(IService).ToString()));
            }
        }

        private class SampleServiceImplementation
        {

        }

        public interface IService
        {            
        }
    }
}